package luke4fun.wheresmymoney.adapter.list.impl;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import luke4fun.wheresmymoney.R;
import luke4fun.wheresmymoney.adapter.list.BasicAdapter;
import luke4fun.wheresmymoney.bean.SearchBean;

public class SearchListAdapter extends BasicAdapter<SearchBean> {

	private List<SearchBean> dataList = new ArrayList<SearchBean>(0);
	private Context context;
	private LayoutInflater layoutInflater;
	private int normalColor;
	private int highLight;
	
	public SearchListAdapter(Context context) {
		this.context = context;
		layoutInflater = LayoutInflater.from(context);
		normalColor = context.getResources().getColor(android.R.color.black);
		highLight   = context.getResources().getColor(android.R.color.holo_red_light);
	}
	
	@Override
	public int getCount() {
		return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		return dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		SearchViewHolder holder;
		if (convertView == null) {
			holder = new SearchViewHolder();
			convertView = layoutInflater.inflate(R.layout.list_search_result, null);
			holder.dateText = (TextView) convertView.findViewById(R.id.date_text);
			holder.itemText = (TextView) convertView.findViewById(R.id.item_text);
			holder.storeText = (TextView) convertView.findViewById(R.id.store_text);
			holder.priceText = (TextView) convertView.findViewById(R.id.price_text);
			holder.categoryText = (TextView) convertView.findViewById(R.id.category_text);
			convertView.setTag(holder);
		} else {
			holder = (SearchViewHolder) convertView.getTag();
			holder.itemText.setTextColor(normalColor);
			holder.storeText.setTextColor(normalColor);
			holder.categoryText.setTextColor(normalColor);
			holder.itemText.setTextAppearance(context, android.R.style.TextAppearance_Holo_SearchResult_Subtitle);
			holder.storeText.setTextAppearance(context, android.R.style.TextAppearance_Holo_SearchResult_Subtitle);
			holder.categoryText.setTextAppearance(context, android.R.style.TextAppearance_Holo_SearchResult_Subtitle);
		}
		
		SearchBean bean = dataList.get(position);
		holder.dateText.setText(bean.getDate());
		holder.priceText.setText(bean.getPrice());
		
		String[] tmp = bean.getItem().split("_@_");
		holder.itemText.setText(tmp[0]);
		if (tmp.length == 2) {
			holder.itemText.setTextAppearance(context, android.R.style.TextAppearance_Holo_SearchResult_Title);
			holder.itemText.setTextColor(highLight);
		}
		
		tmp = bean.getStore().split("_@_");
		holder.storeText.setText(tmp[0]);
		if (tmp.length == 2) {
			holder.storeText.setTextAppearance(context, android.R.style.TextAppearance_Holo_SearchResult_Title);
			holder.storeText.setTextColor(highLight);
		}
		
		tmp = bean.getCategory().split("_@_");
		holder.categoryText.setText(tmp[0]);
		if (tmp.length == 2) {
			holder.categoryText.setTextAppearance(context, android.R.style.TextAppearance_Holo_SearchResult_Title);
			holder.categoryText.setTextColor(highLight);
		}
		
		return convertView;
	}

	@Override
	public void updateList(List<SearchBean> list) {
		dataList.clear();
		dataList.addAll(list);
		notifyDataSetChanged();
	}

	class SearchViewHolder {
		public TextView dateText;
		public TextView itemText;
		public TextView storeText;
		public TextView priceText;
		public TextView categoryText;
	}
}
